let app = getApp()
const db = wx.cloud.database()
const admin = db.collection('adminlist')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    turename: '',
    id: '',
    isLogin: false,
    tabbar: {}
  },

  isLogin () {
    if (!app.globalData.isLogin()) {
      wx.showToast({
        title: '未登录',
        icon: 'error',
        duration: 1500,
        mask: true
      })
      return true
    }
  },

  // 打卡日历界面
  toDaka () {
    if (this.isLogin()) { return }
    wx.navigateTo({
      url: '/pages/daka/daka'
    })
  },

  // 收藏界面
  toCollect () {
    if (this.isLogin()) { return }
    wx.navigateTo({
      url: '/pages/collect/collect'
    })
  },

  // 退出登录
  exit () {
    wx.showModal({
      title: '你要离开了么？',
      cancelText: '留下来',
      cancelColor: '#f00',
      confirmText: '无情退出',
      confirmColor: '#ccc',
      success (res) {
        if (res.confirm) {
          wx.removeStorageSync('loginMsg')
          wx.reLaunch({
            url: '/pages/login/login'
          })
        } else if (res.cancel) {
          wx.showToast({
            title: '我就知道你还是爱轮滑的',
            icon: 'none',
            image: '',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },

  // 去登录
  toLogin () {
    wx.reLaunch({
      url: '/pages/login/login'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    app.editTabbar()
    const thisMsg = wx.getStorageSync('loginMsg') && JSON.parse(wx.getStorageSync('loginMsg'))
    const turename = thisMsg.turename
    const id = thisMsg.id
    this.setData({
      turename,
      id,
      isLogin: app.globalData.isLogin()
    })
  }
})